package com.idbk.chargestation.activity.pile;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.PileAdapter;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonPoint;
import com.idbk.chargestation.bean.JsonPointPile;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.Const;
import com.idbk.chargestation.util.GsonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 预约电桩
 *
 * @author lupc
 */
public class ActivityReservePile extends BaseActivity implements OnClickListener, PileAdapter.OnItemCheckedListener {

    public final static String TAG = ActivityReservePile.class.getSimpleName();
    public final static int REQUEST_BOOKING = 376;
    /**
     * 当前选择的 电桩
     */
    private JsonPointPile mCurrentPile;
    public Context mContext;
    private ListView mListView;
    private List<JsonPointPile> mData;
    private PileAdapter mAdapter;
    private ProgressDialog mDialog;//加载提示框
    private LinearLayout mLayoutEmpty, mLayoutError;
    private TextView mNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_reserve_pile);

        setupView();
    }

    private void setupView() {
        setupToolBar2();//显示标题栏
        mContext = this;

        mLayoutEmpty = (LinearLayout) findViewById(R.id.tj_empty_view);
        mLayoutError = (LinearLayout) findViewById(R.id.tj_error_view);
        if (mLayoutError != null) {
            mLayoutError.setOnClickListener(this);
        }
        mListView = (ListView) findViewById(R.id.listview);
        mData = new ArrayList<JsonPointPile>();
        mAdapter = new PileAdapter(this, mData, mListView, true);
        mAdapter.addOnItemCheckedListener(this);
        mListView.setAdapter(mAdapter);

        mNext = (TextView) findViewById(R.id.textview_next);
        if (mNext != null) {
            mNext.setOnClickListener(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        setupData();
    }

    private void setupData() {
        int id = getIntent().getIntExtra(Const.KEY_ID, -1);
        if (id == -1) {
            Toast.makeText(this, "错误：无法获取参数!", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            mDialog = ProgressDialog.show(mContext, null, "加载中...",
                    true, true, new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            cancelCurrentRequest();
                            finish();
                        }
                    });
            mDialog.setCanceledOnTouchOutside(false);
            //请求充电点详情数据
            mMainRequest = ChargeStationAPI.getPointDetail(id, mResponse);
        }
    }

    private final EHttpResponse mResponse = new EHttpResponse() {

        @Override
        public void onEResponse(String response) {
            JsonPoint result = GsonUtils.toBean(JsonPoint.class, response);
            if (handleResponseStatus(result)) {
                if (result.pointDetails != null) {
                    mData.clear();
                    mNext.setBackgroundResource(R.drawable.bg_main_button_lg_sprite_2);
                    for (JsonPointPile b : result.pointDetails) {
                        if (!b.isEnabled) {
                            continue;
                        }
                        if (!b.bookAvailable) {
                            continue;
                        }
                        if (b.pileType == JsonPointPile.PILE_TYPE_DC
                                || b.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_ASYN
                                || b.pileType == JsonPointPile.PILE_TYPE_DC_DOUBLE_GUN_SYN) {
                            continue;
                        }

                        if (b.pileStatue == JsonPointPile.STATUS_IDLE) {
                            mData.add(b);
                        }
                    }
                    if (mData.size() == 0) {
                        mLayoutEmpty.setVisibility(View.VISIBLE);
                        mLayoutError.setVisibility(View.GONE);
                        mListView.setVisibility(View.GONE);
                        mNext.setBackgroundResource(R.drawable.bg_main_button_sm_black);
                    }
                } else {
                    mLayoutEmpty.setVisibility(View.VISIBLE);
                    mLayoutError.setVisibility(View.GONE);
                    mListView.setVisibility(View.GONE);
                    mNext.setBackgroundResource(R.drawable.bg_main_button_sm_black);
                }
                mAdapter.notifyDataSetChanged();

                if (mData.size() > 0) {
                    mCurrentPile = mData.get(0);
                }
            }
        }

        @Override
        public void onEErrorResponse(VolleyError error) {
            mLayoutEmpty.setVisibility(View.GONE);
            mLayoutError.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
            mNext.setBackgroundResource(R.drawable.bg_main_button_sm_black);
        }

        @Override
        public void onEFinish() {
            super.onEFinish();
            mDialog.dismiss();
        }

    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tj_error_view:
                setupData();
                break;
            case R.id.textview_next:
                if (mCurrentPile == null) {
                    Snackbar.make(mListView, "必须选择一个电桩", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(this, ActivityReservePileCommit.class);
                intent.putExtra(JsonPointPile.KEY, mCurrentPile);
                startActivityForResult(intent, REQUEST_BOOKING);
            default:
                break;
        }
    }

    @Override
    public void onItemChecked(JsonPointPile item) {
        mCurrentPile = item;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            finish();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
